// Purpose : This page is use for add and update blog.
// Date    : 25 Sep, 2007
// Author  : Vinayak Singh Rathore
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SiteManager;
using System.Data.SqlClient;
public partial class DashBoard_myBlog : System.Web.UI.Page
{
    protected int iDoctorId;
    protected void Page_Load(object sender, EventArgs e)
    {
        txtDescription.RootPath = Session["strHttp"] + "includes/Editor/";
        
        // check user is authenticate or not 

        btnDeleteBlog.Attributes.Add("onclick", "return confirmDelete();");
        if (Session["userID"].ToString() != "" && Session["userID"] != null)
        {
            iDoctorId = Convert.ToInt32(Session["userID"]);

        }
        // bind repeater
        if (!IsPostBack)
        {
            //localFunction drchk = new localFunction();
            //drchk.checkDoctorLogin(true);

            localFunction lfunction = new localFunction();
            if (Session["DoctorType"].ToString() == "Doctor")
                lfunction.checkDoctorLogin(true);
            else if (Session["DoctorType"].ToString() == "Dentist")
                lfunction.checkDentistLogin(true);
            else
                Response.Redirect(Session["strHttp"].ToString());

            BindReapeter();
            // If blog is exist for this user Load the blog.
            LoadBlog();
        }
    }
    /// <summary>
    /// Submit button click event handler
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSubmit_Click1(object sender, EventArgs e)
    {
        Manager objManger = new Manager(true,"ConnectionString");
        int userType = 1;
        if ((Session["userType"].ToString() == "Doctor") && ((Convert.ToString(Session["DoctorType"]) == "Doctor")))
            userType = 2;
        else if (Session["userType"].ToString() == "SiteUser")
            userType = 3;
        else if ((Session["userType"].ToString() == "Doctor") && (Convert.ToString(Session["DoctorType"]) == "Dentist"))
            userType = 4;
        SqlParameter[] objSqlParameter = new SqlParameter[6];
        objSqlParameter[0] = new SqlParameter("@Id", SqlDbType.BigInt);
        objSqlParameter[0].Value = 0;
        objSqlParameter[1] = new SqlParameter("@DoctorID", SqlDbType.BigInt);
        objSqlParameter[1].Value = Convert.ToInt32(Session["userID"]);
        objSqlParameter[2] = new SqlParameter("@DoctorType", SqlDbType.BigInt);
        objSqlParameter[2].Value = userType;
        objSqlParameter[3] = new SqlParameter("@BlogMessage", SqlDbType.Text);
     //   objsqlparameter[3].Value = txtDescription.Text;
        objSqlParameter[3].Value = txtDescription.EditorContent;
        objSqlParameter[4] = new SqlParameter("@IsAddOrUpdate", SqlDbType.Bit);
        objSqlParameter[4].Direction = ParameterDirection.Output;
        objSqlParameter[5] = new SqlParameter("@BlogTitle", SqlDbType.Text);
        objSqlParameter[5].Value = txtBlogTitle.Text.ToString().Trim();
        objManger.ExecuteQuery("AddandUpdateBlog", objSqlParameter);
        if (Convert.ToBoolean(objSqlParameter[0].Value))
        {
            lblMessage.Text = "Blog Inserted successfully.";
        }
        else
        {
            lblMessage.Text = "Blog Updated Successfully.";
        }

        objManger = null;
    }
    /// <summary>
    /// public void BindReapeter()
    /// <author>Vinayak Singh Rathore</author>
    /// <date>24 Sep, 2007</date>
    /// </summary>
    public void BindReapeter()
    {
        int userType = 1;
        if ((Session["userType"].ToString() == "Doctor") && ((Convert.ToString(Session["DoctorType"]).Length == 0)))
            userType = 2;
        else if (Session["userType"].ToString() == "SiteUser")
            userType = 3;
        else if ((Session["userType"].ToString() == "Doctor") && (Convert.ToString(Session["DoctorType"]) == "Dentist"))
            userType = 4;
        Manager objManager = new Manager(true,"ConnectionString");
        rpMytopics.DataSource = objManager.ExecuteQuery("select id,topicname from topic where id in (select topicid from forum where authorid=" + iDoctorId + "and authortype=" + userType + ")", "mytopics");
        rpMytopics.DataBind();
    }
    /// <summary>
    /// public void LoadBlog()
    /// </summary>
    public void LoadBlog()
    {
        Manager objManager = new Manager(true,"ConnectionString");
        SqlDataReader objDataReader = null;
        objDataReader = objManager.ExecuteQuery("select * from blog where DoctorId=" + Convert.ToInt32(Session["userID"]));
        objDataReader.Read();
        if (objDataReader.HasRows)
        {
            //txtDescription.Text = objDataReader["BlogMessage"].ToString();
            txtDescription.EditorContent = objDataReader["BlogMessage"].ToString();
            txtBlogTitle.Text = objDataReader["BlogTitle"].ToString();
        }
        objManager = null;
    }
    /// <summary>
    ///  Item command for Repeater controls
    /// </summary>
    /// <author>Vinayak Singh Rathore</author>
    /// <date>22 Sep, 2007</date>
    /// <param name="source"></param>
    /// <param name="e"></param>
    protected void rpMytopics_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "Go")
        {
            Response.Redirect(Session["strHttp"]+"community/Forum.aspx?Id=" + e.CommandArgument);
        }
    }

    // Fucntion to Delete the Blog
    protected void DeleteBlog(object sender, EventArgs e)
    {
        Manager objmanager = new Manager(true,"ConnectionString");
        objmanager.ExecuteScalarQuery("DELETE FROM blog WHERE doctorid = " + Convert.ToInt32(Session["userID"]));
        txtBlogTitle.Text = "";
        txtDescription.EditorContent = "";
        lblMessage.Text = "Blog Deleted Sucessfully";
    }
}
